package com.atguigu.webflux.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

/**
 * @author Jungle
 * @create 2024-01-13 17:32
 */
@Slf4j
@Component
public class MyWebFilter implements WebFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        log.info("==>{}","请求处理方形到目标方法之前");

        // 该处理为异步，目标处理的方法应该在finally中
        Mono<Void> filter = chain.filter(exchange);
        // 如果拆分需要拿新的返回值
        Mono<Void> voidMono = filter.doOnError(err -> {
                    log.info("==>{}", "目标方法异常以后");
                })
                .doFinally(signalType -> {
                    log.info("==>{}", "目标方法执行以后"); //目标方法执行以后
                });
        log.info("==>{}","目标方法执行以后");

        return voidMono; //看清楚返回的是谁
    }
}
